/**
 * 折线图
 */
// 基于准备好的dom，初始化echarts实例
const myChart = echarts.init(document.getElementById('main'));
let date1,date2,date3,data4;
option = {
    title: {
        text: ''
    },
    tooltip: {
        trigger: 'axis'
    },
    legend: {
        data:['光照强度','温度','湿度','湿度','湿度']
    },
    grid: {
        left: '3%',
        right: '4%',
        bottom: '3%',
        containLabel: true
    },
    toolbox: {
        feature: {
            saveAsImage: {}
        }
    },
    xAxis: [
        {
            type: 'category',
            boundaryGap: false,
            data: (

                function data(){
                let now = new Date();
                let res = [];
                let len = 7;
                while (len--) {
                    res.unshift(now.toLocaleTimeString().replace(/^\D*/,''));
                    now = new Date(now - 2000);
                }
                return res;
            })()
        },
    ],
    yAxis: {
        type: 'value'
    },
    series: [
        {
            name:'光照强度',
            type:'line',
            stack: '总量',
            data: (function (){
                let res = [];
                let len = 7;
                while (len--) {
                    res.push(Math.round(Math.random() * 1000));
                }
                return res;
            })()
        },
        {
            name:'温度',
            type:'line',
            stack: '总量',
            data:[220, 182, 191, 234, 290, 330, 310]
        },
        {
            name:'湿度',
            type:'line',
            stack: '总量',
            data:[150, 232, 201, 154, 190, 330, 410]
        }
    ]
};
/*setInterval(function () {
    let axisData = (new Date()).toLocaleTimeString().replace(/^\D*!/, '');
    let data0=option.series[0].data;
    data0.shift();
    data0.push(Math.floor(Math.random()*100));
    option.xAxis[0].data.shift();
    option.xAxis[0].data.push(axisData);
    //将控件添加到容器中
    myChart.setOption(option);

},2100)*/
myChart.setOption(option);
/**
 * 图标中历史数据展示
 */
let stompClient;
$(function () {
    let socket = new SockJS('/gs-guide-websocket');
    stompClient=Stomp.over(socket);
    stompClient.connect({},function () {
        /*温度*/
        stompClient.subscribe("/topic/temp", function (response) {//订阅一个温度数值，希望把数值回调到前端，显示在页面上
            $("#temp").text(response['body'])           //+27.1
        });
    })
})

function RefreshData(){
    //给后台发送一个消息
    $.ajax({
        url: "/RefreshDataBaseMessage",
        type: "get",
        data: {message:""}
    })
}

/**
 * 图表中时间显示
 */
setTimeout(time, 1000);//开始运行
function time(){
    clearTimeout();//清除定时器
    let dt=new Date();
    let y=dt.getFullYear();//获取年
    let mt=dt.getMonth()+1;//获取月
    let day=dt.getDate();
    let h=dt.getHours();
    let m=dt.getMinutes();
    let s=dt.getSeconds();
    document.getElementById("time").innerHTML='当地时间：'+ y + "年" + mt + "月" +day+ "日"+"-"+h+"时"+m+"分"+s+"秒";
    setTimeout(time,1000);
}


